import 'package:flutter/material.dart';
import 'package:flutter_appdemo02/list/chapterone.dart';

class Huadong02 extends StatefulWidget {
  @override
  _Huadong02State createState() => _Huadong02State();
}

class _Huadong02State extends State<Huadong02> {
  ScrollController _controller = new ScrollController();
  bool showToTopBtn = false; //是否显示“返回到顶部”按钮

  @override
  void initState() {
    super.initState();
    //监听滚动事件，打印滚动位置
    _controller.addListener(() {
      if (_controller.offset < 100 && showToTopBtn) {
        setState(() {
          showToTopBtn = false;
        });
      } else if (_controller.offset >= 200 && showToTopBtn == false) {
        setState(() {
          showToTopBtn = true;
        });
      }
    });
  }

  @override
  void dispose() {
    //为了避免内存泄露，需要调用_controller.dispose
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        leading: Icon(
          Icons.account_circle,
          color: Colors.grey,
          size: 34,
        ),
        elevation: 0,
        actions: <Widget>[
          Container(
            margin: EdgeInsets.only(right: 10),
            child: Icon(
              Icons.message,
              color: Colors.grey,
              size: 34,
            ),
          )
        ],
      ),
      body: CustomScrollView(
        shrinkWrap: true,
        slivers: <Widget>[
          SliverList(
            delegate: SliverChildBuilderDelegate(
                  (BuildContext context, int index) {
                return Padding(
                    padding: EdgeInsets.all(15),
                    child: Container(
                      child: Column(
                        children: <Widget>[
                          Column(
                            children: <Widget>[
                              GestureDetector(
                                child: Container(
                                  width: MediaQuery
                                      .of(context)
                                      .size
                                      .width,
                                  height: 500,
                                  decoration: BoxDecoration(
                                      image: DecorationImage(
                                        image: NetworkImage(
                                          chapterone[index]["url"],
                                        ),
                                        fit: BoxFit.cover,
                                      ),
                                      borderRadius: BorderRadius.only(
                                          topRight: Radius.circular(30),
                                          topLeft: Radius.circular(30))),
                                ),
                                onTap: () {
                                  index.toString();
                                },
                              ),
                              Container(
                                height: 60,
                                child: Center(
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.center,
                                    children: <Widget>[
                                      Container(
                                        margin: EdgeInsets.only(right: 40),
                                        child: RaisedButton(
                                          child: Text(
                                            'Jerry,26',
                                            style: TextStyle(
                                                fontWeight: FontWeight.w700,
                                                fontSize: 22),
                                          ),
                                          color: Colors.deepPurpleAccent,
                                          textColor: Colors.white,
                                          elevation: 5,
                                          shape: RoundedRectangleBorder(
                                              borderRadius:
                                              BorderRadius.circular(10)),
                                          onPressed: () {},
                                        ),
                                      ),
                                      Row(
                                        children: <Widget>[
                                          Icon(Icons.location_on),
                                          Text("ChengDu Shi")
                                        ],
                                      ),
                                    ],
                                  ),
                                ),
                                decoration: BoxDecoration(
                                    color: Colors.white,
                                    borderRadius: BorderRadius.only(
                                        bottomRight: Radius.circular(30),
                                        bottomLeft: Radius.circular(30))),
                              )
                            ],
                          ),
                        ],
                      ),
                    ));
              },
              childCount: chapterone.length,
            ),
          ),
        ],
        controller: _controller,
      ),
      backgroundColor: Color.fromRGBO(233, 233, 233, 1),
      floatingActionButton: !showToTopBtn
          ? null
          : FloatingActionButton(
        child: Icon(Icons.arrow_upward),
        onPressed: () {
          _controller.animateTo(.0,
              duration: Duration(milliseconds: 200), curve: Curves.ease);
        },
        backgroundColor: Colors.white,
        foregroundColor: Colors.black,
      ),
    );
  }
}


